﻿using JYH.Business;
using JYH.Model.API;
using JYH.Model;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Security.Policy;
using System.Text;
using System.Threading.Tasks;
using System.Timers;
using System.Windows.Forms;
using Newtonsoft.Json;
using JYH.Framework;
using Newtonsoft.Json.Serialization;
using System.Reflection;

namespace JYH.LED.Monitor
{

    public class LEDMonitor
    {

        private readonly System.Timers.Timer _timer;
        public string MethodName { get; set; }

        /// <summary>
        /// 物料信息同步对象
        /// </summary>
        /// <param name="intervalHours">同步频率 以分钟为单位</param>
        public LEDMonitor(double intervalHours)
        {
     

            // 设置定时器（以小时为单位）
            _timer = new System.Timers.Timer(intervalHours  * 1000);
            _timer.Elapsed += CleanOldFiles;
            _timer.AutoReset = true;
            _timer.Enabled = true;
            // 首次启动时立即执行一次
            CleanOldFiles(null, null);
        }

        private async void CleanOldFiles(object sender, ElapsedEventArgs e)
        {
            try
            {
                await Task.Run(() => Synchronous());
            }
            catch
            {

            }
        }

        public void Start() => _timer.Start();
        public void Stop() => _timer.Stop();


        /// <summary>
        /// 数据同步
        /// </summary>
        /// <returns></returns>
        private async Task Synchronous( )
        {
            string Msg = string.Empty;
            try
            {
                ApiClient apiClient = new ApiClient();
                string Url = "http://127.0.0.1:8080";
                if (ConfigHelper.CheckAppSettingKey("WebAPIServer"))
                {
                    Url = ConfigHelper.GetValue("WebAPIServer");
                }
               
    
                var response = await apiClient.PostAsync(Url + "/api/BAS/GetLampControl");
              
                //object obj = JsonConvert.DeserializeObject(response);


                //// 重新序列化为不带转义符的字符串

                //ResponseJson<BasLampControl> user = JsonConvert.DeserializeObject<ResponseJson<BasLampControl>>(obj.ToString());
                //if (user != null && user.Items != null && user.Items.Count > 0)
                //{
                   
                //}
                
            }
            catch (Exception ex)
            {

                string Path = Application.StartupPath + @"\\Log\\";
                if (!Directory.Exists(Path))
                {
                    Directory.CreateDirectory(Path);
                }
                Path += "\\" + DateTime.Now.ToString("yyyyMMddHHmmss") + ".txt";
                File.WriteAllText(Path, ex.ToString());
            }
        }

        public class IgnoreBackingFieldsResolver : DefaultContractResolver
        {
            protected override List<MemberInfo> GetSerializableMembers(Type objectType)
            {
                var members = base.GetSerializableMembers(objectType);
                return members.Where(m => !m.Name.Contains(">k__BackingField")).ToList();
            }
        }

        // 使用方式
       
    }
}
